﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using RLZY.BLL;

namespace RLZY
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }
        static log4net.ILog LOG = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        BLL.tb_menu bllmenu = new BLL.tb_menu();
        BLL.tb_role bllrole = new BLL.tb_role();
        BLL.tb_rolemenu bllrolemenu = new BLL.tb_rolemenu();
        public static string RoleId;
        public static string M_strName; 
        private void MenuEXIT_Click(object sender, EventArgs e)
        {
            System.Environment.Exit(System.Environment.ExitCode);
            this.Dispose();
            Application.Exit();
        }
        //将infragistics的UltraTabbedMdiManager产生的Mdi Child的Tab头改成中文  
        private void ultraTabbedMdiManager1_InitializeContextMenu(object sender, Infragistics.Win.UltraWinTabbedMdi.MdiTabContextMenuEventArgs e)
        {
            foreach (Infragistics.Win.IGControls.IGMenuItem item in e.ContextMenu.MenuItems)
            {
                switch (item.Text)
                {
                    case "&Close":
                        item.Text = "关闭";
                        break;
                    case "New Hori&zontal Tab Group":
                        item.Text = "新建竖向分组";
                        break;
                    case "New &Vertical Tab Group":
                        item.Text = "新建横向分组";
                        break;
                    case "Move to Ne&xt Tab Group":
                        item.Text = "下一分组";
                        break;
                    case "Move to P&revious Tab Group":
                        item.Text = "上一分组";
                        break;
                    default:
                        break;
                }
            }
        }
        private void 联系作者ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("曾繁林\r\n电话号码：15387904726");
        }

        private void 考勤输入修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //调用子窗体的静态字段（该静态字段就是一个子窗体实例）
            FormBDX_Modify cf = FormBDX_Modify.ChildFromInstanc;
            if (cf != null)
            {
                cf.MdiParent = this;
                cf.Show();//显示子窗体
                cf.Activate();
            }
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            ItemsEnable();
            FormLogin frm = new FormLogin();//创建一个登录窗口
            frm.ShowDialog();//显示这个登录窗口
            if (others.UserHelper.UserID != null)//返回的用户类型
            {
                BLL.userpassword bllpwd = new BLL.userpassword();
                Model.userpassword model = bllpwd.GetModel(Convert.ToInt32(others.UserHelper.UserID));
                权限 = model.备注;
                RoleId = model.roleid.ToString();
                //username = frm.userName;
                //这里添加修改菜单属性代码！
                this.getSubItem(变动项考勤管理ToolStripMenuItem);
                this.getSubItem(固定项管理ToolStripMenuItem);
                this.getSubItem(工资津贴调整ToolStripMenuItem);
                this.getSubItem(工资查询ToolStripMenuItem);

                this.getSubItem(工资打印ToolStripMenuItem);
                this.getSubItem(公积金管理ToolStripMenuItem);
                this.getSubItem(信息管理ToolStripMenuItem);
                this.getSubItem(帮助ToolStripMenuItem);

                //this.getSubItem(物资管理ToolStripMenuItem);
                
                SetMenuItemByRole();
               
            }
        }
        public void ItemsEnable()
        {
            this.变动项考勤管理ToolStripMenuItem.Enabled = false;
            this.固定项管理ToolStripMenuItem.Enabled = false;
            this.工资津贴调整ToolStripMenuItem.Enabled = false;
            this.工资查询ToolStripMenuItem.Enabled = false;
            this.工资打印ToolStripMenuItem.Enabled = false;
            this.公积金管理ToolStripMenuItem.Enabled = false;
            this.信息管理ToolStripMenuItem.Enabled = false;
            this.帮助ToolStripMenuItem.Enabled = false;
            
        }
        //****************************************************************************************************
        //
        // 功能：初始化菜单项，全部设置为不可用。
        // 函数：InitMenuItem(),InitSubMenuItem(ToolStripItem item)
        //
        //                                                          qjs(jothan)
        //                                                        2008/12/11
        //
        //*****************************************************************************************************
        //递归初始化菜单
        public void getSubItem(ToolStripMenuItem item)
        {
            item.Enabled = false;
            if (item.DropDownItems.Count > 0)
            {
                for (int i = 0; i < item.DropDownItems.Count; i++)
                {
                    if (item.DropDownItems[i] is ToolStripMenuItem)
                    {
                        ToolStripMenuItem sdc = item.DropDownItems[i] as ToolStripMenuItem;
                        item.DropDownItems[i].Enabled = false;
                        getSubItem(sdc);
                    }
                }
            }
        }
        //****************************************************************************************************
        //
        // 功能：根据用户在用户权限表中的权限动态的设置能使用的菜单项。
        // 函数：SetMenuItemByRole(),SetSubMenuItemByRole()
        //
        //                                                          qjs(jothan)
        //                                                        2008/12/11
        //
        //*****************************************************************************************************

        //对一级菜单进行权限设置
        private void SetMenuItemByRole()
        {


            StringBuilder sb = new StringBuilder();
            sb.Append("id in" + "(select menuid from tb_rolemenu where roleid='" + RoleId + "' and fmuid = 0)");

            DataSet ds = bllmenu.GetList(sb.ToString());
            DataTable dt = ds.Tables[0];

            //for (int i = 0; i < dt.Rows.Count; i++)
            //{
            //    ToolStripMenuItem toolItem = new ToolStripMenuItem();
            //    toolItem.Name = dt.Rows[i][3].ToString ();
            //    menuStrip2.Items[toolItem.Name].Enabled = true;//各一级菜单是主菜单menuStrip2集合的项
            //    SetSubMenuItemByRole(menuStrip2.Items[toolItem.Name]);//将一级菜单对应主菜单menuStrip2集合的项传给子菜单设置函数
            //}
            foreach (DataRow dr in dt.Rows)
            {
                ToolStripMenuItem toolItem = new ToolStripMenuItem();

                toolItem.Name = dr[3].ToString();//一级菜单的menuname
                //string a = toolItem.Name.ToString();
                menuStrip2.Items[toolItem.Name].Enabled = true;//各一级菜单是主菜单menuStrip2集合的项
                SetSubMenuItemByRole(menuStrip2.Items[toolItem.Name]);//将一级菜单对应主菜单menuStrip2集合的项传给子菜单设置函数
            }
        }

        //对一级菜单的所有子菜单进行设置
        private void SetSubMenuItemByRole(ToolStripItem item)
        {
            string mname = item.Name;

            ToolStripMenuItem pItem = (ToolStripMenuItem)item;

            //根据父菜单项加载子菜单
            StringBuilder sb = new StringBuilder();
            sb.Append("fmuname ='" + mname + "' and id in (select menuid from tb_rolemenu where roleid='" + RoleId + "')");
            //string sql = "fmuname ='" + mname + "' and id in (select menuid from tb_rolemenu where roleid='" + RoleId + "')";
            DataSet ds = bllmenu.GetList(sb.ToString());
            DataTable dt = ds.Tables[0];


            if (dt.Rows.Count != 0)
            {

                foreach (DataRow dr in dt.Rows)
                {
                    ToolStripMenuItem subItem = new ToolStripMenuItem();
                    subItem.Name = dr[3].ToString();

                    try
                    {
                        pItem.DropDownItems[subItem.Name].Enabled = true;

                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(e.Message);
                    }
                }
            }
            else
            {

            }

        }
        
        public string userID{ get; set; }

        private void 错误日志ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(Application.StartupPath + "\\log\\log.txt");
        }

        private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult dr;
            dr = MessageBox.Show("确定要退出吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.OK)
            {
                e.Cancel = false;
                System.Environment.Exit(System.Environment.ExitCode);
                this.Dispose();
                Application.Exit();
            }
            else
            {
                e.Cancel = true;
            }
        }

        private void 变动项显示ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //调用子窗体的静态字段（该静态字段就是一个子窗体实例）
            FormBDX_Show cf = FormBDX_Show.ChildFromInstanc;
            if (cf != null)
            {
                cf.MdiParent = this;
                cf.Show();//显示子窗体
                cf.Activate();
            }
        }

        private void 数据保护ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //调用子窗体的静态字段（该静态字段就是一个子窗体实例）
            FormGZBH cf = FormGZBH.ChildFromInstanc;
            if (cf != null)
            {
                cf.MdiParent = this;
                cf.Show();//显示子窗体
                cf.Activate();
            }
        }

        private void 新增员工输入ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //调用子窗体的静态字段（该静态字段就是一个子窗体实例）
            FormNotKey_GDX_Add cf = FormNotKey_GDX_Add.ChildFromInstanc;
            if (cf != null)
            {
                cf.MdiParent = this;
                cf.Show();//显示子窗体
                cf.Activate();
            }

        }

        private void 员工减少删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //调用子窗体的静态字段（该静态字段就是一个子窗体实例）
            FormNotKey_GDX_Del cf = FormNotKey_GDX_Del.ChildFromInstanc;
            if (cf != null)
            {
                cf.MdiParent = this;
                cf.Show();//显示子窗体
                cf.Activate();
            }
        }

        private void 关键固定项修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {//调用子窗体的静态字段（该静态字段就是一个子窗体实例）
            FormKey_GDX_Modify cf = FormKey_GDX_Modify.ChildFromInstanc;
            if (cf != null)
            {
                cf.MdiParent = this;
                cf.Show();//显示子窗体
                cf.Activate();
            }
        }

        private void 非关键固定项修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //调用子窗体的静态字段（该静态字段就是一个子窗体实例）
            FormNotKey_GDX_Modify cf = FormNotKey_GDX_Modify.ChildFromInstanc;
            if (cf != null)
            {
                cf.MdiParent = this;
                cf.Show();//显示子窗体
                cf.Activate();
            }
        }

        private void 固定项显示ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //调用子窗体的静态字段（该静态字段就是一个子窗体实例）
            FormKey_GDX_Show cf = FormKey_GDX_Show.ChildFromInstanc;
            if (cf != null)
            {
                cf.MdiParent = this;
                cf.Show();//显示子窗体
                cf.Activate();
            }
        }

        private void 工资津贴调整ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //调用子窗体的静态字段（该静态字段就是一个子窗体实例）
            FormGL_JT_Modify cf = FormGL_JT_Modify.ChildFromInstanc;
            if (cf != null)
            {
                cf.MdiParent = this;
                cf.Show();//显示子窗体
                cf.Activate();
            }
        }

        private void 工资查询ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            //调用子窗体的静态字段（该静态字段就是一个子窗体实例）
            FormGZ_Show cf = FormGZ_Show.ChildFromInstanc;
            if (cf != null)
            {
                cf.MdiParent = this;
                cf.Show();//显示子窗体
                cf.Activate();
            }
        }

        private void 相邻2月对照ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormXL_2Moon_DZ cf = FormXL_2Moon_DZ.ChildFromInstanc;
            if (cf != null)
            {
                cf.MdiParent = this;
                cf.Show();//显示子窗体
                cf.Activate();
            }
        }

        private void 离岗员工查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormLG_GZ_Show cf = FormLG_GZ_Show.ChildFromInstanc;
            if (cf != null)
            {
                cf.MdiParent = this;
                cf.Show();//显示子窗体
                cf.Activate();
            }
        }

        private void 将要内退员工显示ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormNT_Show cf = FormNT_Show.ChildFromInstanc;
            if (cf != null)
            {
                cf.MdiParent = this;
                cf.Show();//显示子窗体
                cf.Activate();
            }
        }

        private void 人员分布表ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormRYFB cf = FormRYFB.ChildFromInstanc;
            if (cf != null)
            {
                cf.MdiParent = this;
                cf.Show();//显示子窗体
                cf.Activate();
            }
        }

        private void 四金统计表ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormSJTJB cf = FormSJTJB.ChildFromInstanc;
            if (cf != null)
            {
                cf.MdiParent = this;
                cf.Show();//显示子窗体
                cf.Activate();
            }
        }

        private void 新余公积金表ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormGJJB cf = FormGJJB.ChildFromInstanc;
            if (cf != null)
            {
                cf.MdiParent = this;
                cf.Show();//显示子窗体
                cf.Activate();
            }
        }

        private void 测奖金空表ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormCJJKB cf = FormCJJKB.ChildFromInstanc;
            if (cf != null)
            {
                cf.MdiParent = this;
                cf.Show();//显示子窗体
                cf.Activate();
            }
        }

        private void 工资清单打印ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormGZ_Print cf = FormGZ_Print.ChildFromInstanc;
            if (cf != null)
            {
                cf.MdiParent = this;
                cf.Show();//显示子窗体
                cf.Activate();
            }
        }

        private void 五种用工汇总打印ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormGZSUM_Print cf = FormGZSUM_Print.ChildFromInstanc;
            if (cf != null)
            {
                cf.MdiParent = this;
                cf.Show();//显示子窗体
                cf.Activate();
            }
        }

        private void 特钢本部汇总打印ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormGZSUM_TG_Print cf = FormGZSUM_TG_Print.ChildFromInstanc;
            if (cf != null)
            {
                cf.MdiParent = this;
                cf.Show();//显示子窗体
                cf.Activate();
            }
        }

        private void 工资申请表打印ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormGZ_SQ_Print cf = FormGZ_SQ_Print.ChildFromInstanc;
            if (cf != null)
            {
                cf.MdiParent = this;
                cf.Show();//显示子窗体
                cf.Activate();
            }
        }

        private void 特钢本部申请表打印ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormGZ_SQ_TG_Print cf = FormGZ_SQ_TG_Print.ChildFromInstanc;
            if (cf != null)
            {
                cf.MdiParent = this;
                cf.Show();//显示子窗体
                cf.Activate();
            }
        }

        private void 工资清单打印2ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormGZ_Print_2 cf = FormGZ_Print_2.ChildFromInstanc;
            if (cf != null)
            {
                cf.MdiParent = this;
                cf.Show();//显示子窗体
                cf.Activate();
            }
        }

        private void 各部门单位工资打印2ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormGZSUM_Print_2 cf = FormGZSUM_Print_2.ChildFromInstanc;
            if (cf != null)
            {
                cf.MdiParent = this;
                cf.Show();//显示子窗体
                cf.Activate();
            }
        }

        private void 特钢本部汇兑2打印ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormGZSUM_TG_Print_2 cf = FormGZSUM_TG_Print_2.ChildFromInstanc;
            if (cf != null)
            {
                cf.MdiParent = this;
                cf.Show();//显示子窗体
                cf.Activate();
            }
        }

        private void 公积金取出新增ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormGJJ_out_Add cf = FormGJJ_out_Add.ChildFromInstanc;
            if (cf != null)
            {
                cf.MdiParent = this;
                cf.Show();//显示子窗体
                cf.Activate();
            }
        }

        private void 公积金取出删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormGJJ_out_Del cf = FormGJJ_out_Del.ChildFromInstanc;
            if (cf != null)
            {
                cf.MdiParent = this;
                cf.Show();//显示子窗体
                cf.Activate();
            }
        }

        private void 公积金入账显示ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormGJJ_RZ_Show cf = FormGJJ_RZ_Show.ChildFromInstanc;
            if (cf != null)
            {
                cf.MdiParent = this;
                cf.Show();//显示子窗体
                cf.Activate();
            }
        }

        private void 信息查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ForminFormation_Show cf = ForminFormation_Show.ChildFromInstanc;
            if (cf != null)
            {
                cf.MdiParent = this;
                cf.Show();//显示子窗体
                cf.Activate();
            }
        }

        private void 权限管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form_Authority cf = Form_Authority.ChildFromInstanc;
            if (cf != null)
            {
                cf.MdiParent = this;
                cf.Show();//显示子窗体
                cf.Activate();
            }
        }

        public string 权限 { get; set; }
    }
}
